// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "todoDatabase.js" as Storage
import "CalendarJS.js" as CJS


Rectangle {
    id: dayView;
    width:300;
    height:500;
    border.width: 2;
    border.color: "black";
    //color:"orange";
    anchors.centerIn: parent
    property string lang

    property variant today : new Date();

    property int _month: today.getMonth()+1;
    property int _day: today.getDate();
    property int _year: today.getFullYear();
    property variant dateString:  _month+"_"+_day+"_"+_year;
    property variant stringVarWhat;
    property variant stringVarDue;
    property variant stringVarPriority;


    Rectangle{
        id:title;
        width:parent.width;
        height:50;
        color:"#207200";

        Rectangle{
            anchors.right: parent.right;
            id:close;
            width:20;
            height:20;
            color:"#207200";

            Image{
                source:"Assets/Calendar/removeL.png"
            }


            MouseArea{
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton;
                onClicked:{
                    //console.log("Clicked" + (index-CalFunctions.first_DOW+1));
                    onClicked: dayView.visible = false;
                }
            }
        }

        Text{
            id:toDoDate;
            anchors.centerIn: parent;
            //property int month: CalFunctions.monthIndex;
           // text: CalFunctions.monthIndex+1 + "/" +CalFunctions.day + "/" + CalFunctions.year;
            text: _month+"/"+_day+"/"+_year;
            font.bold: true;
            font.pointSize: 10;
        }
    }

    Rectangle{
        id:body;
        width:parent.width;
        height: 400; // main height - title height - add new height
       color:"#bfbfbf";
        anchors.top:title.bottom;

            Component.onCompleted: {
                // Initialize the database
                Storage.initialize();
                // Sets a value in the database
                //Storage.addDate("4_13_2012","TESTABCDE","4/26/2012","HIGH");        //function setSetting(date, what, due, priority) {
                // Sets the textDisplay element's text to the value we just set
                //Storage.deleteDate("4_12_2012",""

                stringVarWhat =Storage.getDate(dateString);
                stringVarDue =Storage.getDue(dateString);
                stringVarPriority =Storage.getPriority(dateString);
                populate();
            }
            Component{
                id:toDoDelegate;
                Column{
                    id:toDoColumn
                    spacing:1;
                        Row{
                            spacing:0
                            Rectangle{
                                id:whatCol;
                                width:150;
                                border.color: "black"
                                border.width: 1;
                                height:30;
                                color:"#9dc488";
                                Text{
                                    anchors.centerIn: parent;
                                    text: what
                                    font.bold: true;
                                    font.pointSize: 10;
                                }
                            }
                            Rectangle{
                                id:dueCol;
                                width:75;
                                height:30;
                                border.color: "black"
                                border.width: 1;
                                color:"#9dc488";
                                Text{
                                    anchors.centerIn: parent;
                                    text: due
                                    font.bold: true;
                                    font.pointSize: 10;
                                }
                            }
                            Rectangle{
                                id:priorityCol;
                                width:75;
                                height:30;
                                border.color: "black"
                                border.width: 1;
                                color:"#9dc488";
                                Text{
                                     anchors.centerIn: parent;
                                    text: priority
                                    font.bold: true;
                                    font.pointSize: 10;
                                }
                                Rectangle{
                                    id:deleteToDo;
                                    width: 10;
                                    height: 10;
                                    color:"#9dc488";
                                    anchors.right: parent.right
                                    anchors.top: parent.top;
                                    anchors.topMargin: 1;
                                    visible: if(index!=0){
                                                 true;
                                             }
                                            else
                                                 false;

                                    Image{
                                        source:"Assets/Calendar/remove.png"
                                    }


                                }

                                MouseArea{
                                    id: removeEvent;
                                    width:parent.width
                                    height: parent.height
                                    onClicked:{removeItem(dateString,toDoList.get(index).what, index);}
                                }
                            }
                        }

                    }
            }



    Rectangle{
        id:addNew;
        width: parent.width;
        height: 50;
        color:"#207200";
        anchors.top: body.bottom; //Can do toDoColumn.bottom to have it resize itself.
        border.color: "black"
        border.width: 1;


        Text{
                anchors.centerIn: parent
                text: if(lang=="1")"Add New"; else "añadir nuevos"
                font.bold: true;
                font.pointSize: 10;

        }
        MouseArea{
            id: addEventClick;
            width:parent.width
            height: parent.height
            onClicked:{userInput.visible=true;}
        }


    }


    ListView{
        id:toDoView;
        anchors.fill: parent
        anchors.top: body.bottom;
        model: toDoList;
        delegate: toDoDelegate
        }
    ListModel {
         id: toDoList

         ListElement{
             what: "What"
             due: "Due"
             priority: "Priority"
         }
     }
    }
    Rectangle{
        id: userInput
        anchors.centerIn: parent;
        height: 50
        //color: "#00000000"
        border.color: "black";
        border.width: 1;
        width: 300
        visible:false;
        TextInput{
            id: userTextWhat
            anchors.centerIn: parent;
            width: parent.width;
            height: 50
            text: {if(lang=="1")qsTr("Enter 'What' Here");else qsTr("entrar lo que");}
            horizontalAlignment: TextInput.AlignHCenter
            font.pixelSize: 15
        }

        Rectangle{
            id:userInput2
            anchors.top: parent.bottom;
           // anchors.topMargin: 1
            border.color: "black";
            border.width: 1;
            height:50
            width: parent.width;
            TextInput{
                id: userTextDue
                anchors.centerIn: parent;
                width: parent.width;
                height: 50
                text: {if(lang=="1")qsTr("Enter 'Due Date' Here"); else qsTr("fecha de vencimiento");}
                horizontalAlignment: TextInput.AlignHCenter
                font.pixelSize: 15
            }
        }

        Rectangle{
            id:userInput3
            anchors.top: userInput2.bottom;
            //anchors.topMargin: 1
            border.color: "black";
            border.width: 1;
            height:50
            width: parent.width;
            TextInput{
                id: userTextPriority
                anchors.centerIn: parent;
                width: parent.width;
                height: 50
                text: {if(lang=="1")qsTr("Enter 'Priority' Here");else qsTr("prioridad");}
                horizontalAlignment: TextInput.AlignHCenter
                font.pixelSize: 15
            }
        }

        Rectangle{
            //color: "#5aff00"
           // border.color: "black";
           // border.width: 1;
            width: 25
            height: 25
            anchors.right: userInput3.right;
            anchors.bottom: userInput3.bottom;

            Image{
                source:"Assets/Calendar/add.png"
            }

            MouseArea{
                id: addEvent;
                width:parent.width
                height: parent.height
                onClicked:{addItem();userInput.visible=false;}
            }
        }
    }

    function removeItem(date, what, index){

        if(index!==0){
            Storage.deleteDate(date, what)
            toDoList.remove(index);
        }
    }

    function populate(){
        for(var i=0; i<stringVarWhat.length; i++){
            //console.log("Assests/Calendar/"+stringVar.length);
            toDoList.append({"what":stringVarWhat[i], "due":stringVarDue[i], "priority":stringVarPriority[i]});
        }

    }

    function getFileName(){
        console.log("Assests/Calendar/"+_month +"_" + _day +"_"+_year+".xml");
        return "Assests/Calendar/"+_month +"_" + _day +"_"+_year+".xml";

    }

    function addItem(){
        Storage.addDate(_month +"_" + _day +"_"+_year,userTextWhat.text,userTextDue.text,userTextPriority.text);
        toDoList.append({"what":userTextWhat.text, "due":userTextDue.text, "priority":userTextPriority.text});
        console.log("TEST: " + _month +"_" + _day +"_"+_year,userTextWhat.text,userTextDue.text,userTextPriority.text);
    }

    function setDate(month, day, year){
        _month = month+1;
        _day = day;
        _year = year;
        //if(month===1){
         //   month++
          //  toDoDate.text = month + "/" +day + "/" + year;
       // }
       // else
        //console.log("HERE: " + month);
             dateString = _month+"_"+_day+"_"+_year;
             toDoDate.text = month+1 + "/" +day + "/" + year;

        toDoList.clear();
        console.log(dateString);
        toDoList.append({"what":"What", "due":"Due", "priority":"Priority"});
        stringVarWhat =Storage.getDate(dateString);
        stringVarDue =Storage.getDue(dateString);
        stringVarPriority =Storage.getPriority(dateString);
        populate();

    }
    }


//DAN


